package com.example.base;

import java.util.Random;

public class ConstructTest {
    public static void main(String[] args) {
        var staff = new Employee2[3];
        staff[0] = new Employee2("Harry", 40000);
        staff[1] = new Employee2(60000);
        staff[2] = new Employee2();

        for(Employee2 e : staff){
            System.out.println("name = " + e.getName() + ", salary = " + e.getSalary() + ", id = " + e.getId());
        }
    }
}


class Employee2 {
    private static int nextId;
    private int id;
    private String name;
    private double salary;
    //静态初始化块，只执行一次
    static {
        var generator = new Random();
        nextId = generator.nextInt(10000);
    }
    //对象初始化块，对象创建的时候执行一次
    {
        id = nextId;
        nextId ++;
    }
    public Employee2(String n, double s){
        name = n;
        salary = s;
    }
    public Employee2(double s){
        this("Employee #", s);
    }

    public Employee2(){

    }
    public String getName(){
        return name;
    }
    public double getSalary(){
        return salary;
    }
    public int getId(){
        return id;
    }
}